<template>
  <div class="app-container patient-box">
    <div class="search_wrap">
      <div class="single_top_search" id="search">
        <div class="breadcrumb_area">
          <el-breadcrumb>
            <!-- <el-breadcrumb-item to="index">装配图管理</el-breadcrumb-item> -->
            <el-breadcrumb-item>患者信息</el-breadcrumb-item>
          </el-breadcrumb>
        </div>
        <el-row>
          <el-form :model="queryParams" ref="queryForm" label-width="100px" label-position="right">
            <el-col :span="6">
              <el-form-item label="姓名" prop="postCode" class="search-item">
                <el-input
                  v-model="queryParams.postCode"
                  placeholder="请输入姓名"
                  clearable
                  @keyup.enter.native="handleQuery"
                />
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="年龄" prop="postName" class="search-item">
                <el-input
                  v-model="queryParams.postName"
                  placeholder="请输入年龄"
                  clearable
                  @keyup.enter.native="handleQuery"
                />
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="手机号" prop="postName" class="search-item">
                <el-input
                  v-model="queryParams.postName"
                  placeholder="请输入手机号"
                  clearable
                  @keyup.enter.native="handleQuery"
                />
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="管理机构" prop="postName" class="search-item">
                <el-input
                  v-model="queryParams.postName"
                  placeholder="请输入管理机构"
                  clearable
                  @keyup.enter.native="handleQuery"
                />
              </el-form-item>
            </el-col>
            <el-col :span="24" class="btnFloat">
              <el-button icon="el-icon-search" class="icon_btns" @click="handleQuery"></el-button>
              <el-button icon="el-icon-refresh" class="icon_btns white" @click="resetQuery"></el-button>
            </el-col>
          </el-form>
        </el-row>
      </div>
		</div>
    <div class="tools_wrap" style="margin: 15px 20px;">
      <div class="left_tools">
        <el-button icon="el-icon-plus" class="text_btns">新增</el-button>
        <el-button class="text_btns">导入图号</el-button>
      </div>
      <div class="right_tools">
        <el-button class="text_btns" @click="codeDialog = true">二维码</el-button>
        <el-button class="text_btns border" @click="exportDialog = true">导出</el-button>
      </div>
    </div>

    <!-- <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:post:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:post:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:post:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:post:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row> -->
    <div class="table-box">
      <el-table v-loading="loading" :data="postList" @selection-change="handleSelectionChange">
        <el-table-column label="序号" width="60" type="index" align="center"></el-table-column>
        <el-table-column label="姓名" align="center" prop="postId" width="60"/>
        <el-table-column label="年龄" align="center" prop="postCode" width="60"/>
        <el-table-column label="手机号" align="center" prop="postCode" width="100"/>
        <el-table-column label="管理机构" align="center" prop="postCode" width="100"/>
        <el-table-column label="管理团队" align="center" prop="postCode" width="100"/>
        <el-table-column label="用户类型" align="center" prop="postId" width="100">
          <template slot-scope="scope">
            <span class="tag new" v-if="scope.row.postId === 1">新用户</span>
            <span class="tag old" v-else>老用户</span>
          </template>
        </el-table-column>
        <el-table-column label="随访量表" align="center" prop="postCode" width="100">
          <template slot-scope="scope">
            <span>0/5</span>
          </template>
        </el-table-column>
        <el-table-column label="家庭环境" align="center" prop="postName">
          <template slot-scope="scope">
            <span class="text-color">{{ scope.row.postName }}</span>
          </template>
        </el-table-column>
        <el-table-column label="用药记录" align="center" prop="postSort">
          <template slot-scope="scope">
            <span class="text-color">{{ scope.row.postSort }}</span>
          </template>
        </el-table-column>
        <el-table-column label="注册时间" align="center" prop="createTime" width="180">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.createTime) }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['system:post:edit']"
            >修改</el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['system:post:remove']"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改岗位对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="岗位名称" prop="postName">
          <el-input v-model="form.postName" placeholder="请输入岗位名称" />
        </el-form-item>
        <el-form-item label="岗位编码" prop="postCode">
          <el-input v-model="form.postCode" placeholder="请输入编码名称" />
        </el-form-item>
        <el-form-item label="岗位顺序" prop="postSort">
          <el-input-number v-model="form.postSort" controls-position="right" :min="0" />
        </el-form-item>
        <el-form-item label="岗位状态" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio
              v-for="dict in dict.type.sys_normal_disable"
              :key="dict.value"
              :label="dict.value"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

   <!-- 复制二维码 -->
    <el-dialog
      title="二维码"
      :visible.sync="codeDialog"
      width="30%">
      <div class="code-input">
        <el-input :disabled="true" v-model="codeUrl"></el-input>
        <el-button class="text_btns" @click="copy">复制链接</el-button>
      </div>
      <span>复制二维码链接分享,可生成H5页面</span>
    </el-dialog>

    <!-- 导出申请 -->
    <el-dialog
      title="导出"
      :visible.sync="exportDialog"
      width="30%">
      <div class="dialog-content">
        <i class="el-icon-warning-outline"></i>
        <p>导出患者信息表需要提交申请，申请信息在“我的发起”中可查看进度！</p>
        <span slot="footer" class="dialog-footer">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="dialogVisible = false">确 定</el-button>
        </span>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listPost, getPost, delPost, addPost, updatePost } from "@/api/system/post";

export default {
  name: "Post",
  dicts: ['sys_normal_disable'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 岗位表格数据
      postList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        postCode: undefined,
        postName: undefined,
        status: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        postName: [
          { required: true, message: "岗位名称不能为空", trigger: "blur" }
        ],
        postCode: [
          { required: true, message: "岗位编码不能为空", trigger: "blur" }
        ],
        postSort: [
          { required: true, message: "岗位顺序不能为空", trigger: "blur" }
        ]
      },
      codeDialog: false,
      codeUrl: 'http://gphc.yj-med.com/#/loginpad',
      exportDialog: false
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询岗位列表 */
    getList() {
      this.loading = true;
      listPost(this.queryParams).then(response => {
        this.postList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        postId: undefined,
        postCode: undefined,
        postName: undefined,
        postSort: 0,
        status: "0",
        remark: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.postId)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加岗位";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const postId = row.postId || this.ids
      getPost(postId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改岗位";
      });
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.postId != undefined) {
            updatePost(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addPost(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const postIds = row.postId || this.ids;
      this.$modal.confirm('是否确认删除岗位编号为"' + postIds + '"的数据项？').then(function() {
        return delPost(postIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/post/export', {
        ...this.queryParams
      }, `post_${new Date().getTime()}.xlsx`)
    },
    copy() {
      navigator.clipboard.writeText(this.codeUrl)
      .then(() => {
        this.$modal.msgSuccess("复制成功");
      })
      .catch(()=> {
        this.$modal.msgError("复制失败");
      });
    }
  }
};
</script>

<style lang="scss" scoped>
.patient-box {
  .search_wrap {
    background: #fff;
    border-radius: 8px;
    padding: 0 0 15px 0;
  }

  .table-box {
    padding: 16px;
    background: #fff;
    margin-top: 24px;
    border-radius: 8px;
    .tag {
      width: 69px;
      height: 30px;
      border-radius: 4px;
      display: inline-block;
      line-height: 30px;
    }
    .new {
      border: 1px solid #1890FF;
      color: #1890FF;
    }
    .old {
      border: 1px solid #FF7A40;
      color: #FF7A40;
    }
    .text-color {
      color: #A83237;
    }
  }

  .code-input {
    display: flex;
    margin: 26px 0;
    ::v-deep .el-input__inner {
      height: 40px;
    }
    button {
      margin-left: 10px;
    }
  }

  .dialog-content {
    display: flex;
    flex-direction: column;
    align-items: center;
    padding: 20px 34px;
  }
}
</style>